/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ExerciciosListaDialog.java
 *
 * Created on 03/11/2009, 01:27:38
 */
package interpretadorportugolvisual;

import br.org.isvi.interpretador.portugol.access.IPVBusinessFacade;
import br.org.isvi.interpretador.portugol.model.Aula;
import br.org.isvi.interpretador.portugol.model.Exercicio;
import br.org.isvi.interpretador.portugol.model.Nivel;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author abraaoisvioliveirasouzadocarmo
 */
public class ExerciciosListaDialog extends javax.swing.JDialog {

    /** Creates new form ExerciciosListaDialog */
    public ExerciciosListaDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblExerciciosAula = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cmbNivel = new javax.swing.JComboBox();
        btnFiltrar = new javax.swing.JButton();
        btnTodos = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblExerciciosAvulsos = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        tblExerciciosAula.setModel(obterExerciciosAula());
        tblExerciciosAula.setName("tblExerciciosAula"); // NOI18N
        tblExerciciosAula.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblExerciciosAulaMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblExerciciosAula);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(interpretadorportugolvisual.InterpretadorPortugolVisualApp.class).getContext().getResourceMap(ExerciciosListaDialog.class);
        tblExerciciosAula.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("tblExerciciosAula.columnModel.title0")); // NOI18N
        tblExerciciosAula.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("tblExerciciosAula.columnModel.title1")); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 494, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 486, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab(resourceMap.getString("jPanel1.TabConstraints.tabTitle"), jPanel1); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N

        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setName("jSplitPane1"); // NOI18N

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel3.border.title"))); // NOI18N
        jPanel3.setName("jPanel3"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        cmbNivel.setModel(new DefaultComboBoxModel(IPVBusinessFacade.obterTodosNiveis().toArray()));
        cmbNivel.setName("cmbNivel"); // NOI18N

        btnFiltrar.setText(resourceMap.getString("btnFiltrar.text")); // NOI18N
        btnFiltrar.setName("btnFiltrar"); // NOI18N
        btnFiltrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFiltrarActionPerformed(evt);
            }
        });

        btnTodos.setText(resourceMap.getString("btnTodos.text")); // NOI18N
        btnTodos.setName("btnTodos"); // NOI18N
        btnTodos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTodosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbNivel, 0, 412, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(btnTodos)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnFiltrar)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cmbNivel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnFiltrar)
                    .addComponent(btnTodos))
                .addContainerGap())
        );

        jSplitPane1.setLeftComponent(jPanel3);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tblExerciciosAvulsos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Problema", "Nivel"
            }
        ));
        tblExerciciosAvulsos.setName("tblExerciciosAvulsos"); // NOI18N
        tblExerciciosAvulsos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblExerciciosAvulsosMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblExerciciosAvulsos);
        tblExerciciosAvulsos.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("tblExerciciosAvulsos.columnModel.title0")); // NOI18N
        tblExerciciosAvulsos.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("tblExerciciosAvulsos.columnModel.title1")); // NOI18N

        jSplitPane1.setRightComponent(jScrollPane1);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 482, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 474, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab(resourceMap.getString("jPanel2.TabConstraints.tabTitle"), jPanel2); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 515, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 532, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tblExerciciosAulaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblExerciciosAulaMouseClicked
        SessaoAluno.getInstance().exercicioSelecionado = (Exercicio) ((DefaultTableModel) tblExerciciosAula.getModel()).getValueAt(tblExerciciosAula.getSelectedRow(), 0);
        new ProblemaDialog(null, true).setTexto(SessaoAluno.getInstance().exercicioSelecionado.getProblema()).setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_tblExerciciosAulaMouseClicked

    private void btnFiltrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFiltrarActionPerformed
        tblExerciciosAvulsos.setModel(obterExerciciosNivel((Nivel) cmbNivel.getSelectedItem()));
    }//GEN-LAST:event_btnFiltrarActionPerformed

    private void btnTodosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTodosActionPerformed
        tblExerciciosAvulsos.setModel(obterTodosExercicios());
    }//GEN-LAST:event_btnTodosActionPerformed

    private void tblExerciciosAvulsosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblExerciciosAvulsosMouseClicked
        SessaoAluno.getInstance().exercicioSelecionado = (Exercicio) ((DefaultTableModel) tblExerciciosAvulsos.getModel()).getValueAt(tblExerciciosAvulsos.getSelectedRow(), 0);
        new ProblemaDialog(null, true).setTexto(SessaoAluno.getInstance().exercicioSelecionado.getProblema()).setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_tblExerciciosAvulsosMouseClicked

    public DefaultTableModel obterTodosExercicios() {
        Exercicio[][] Exercicios = null;
        Exercicio e = new Exercicio();


        java.util.List<Exercicio> lst = IPVBusinessFacade.obterTodosExercicio();
        Exercicios = new Exercicio[lst.size()][2];

        for (int i = 0; i < Exercicios.length; i++) {
            Exercicios[i] = new Exercicio[]{lst.get(i)};
        }



        return new ExercicioTableModel(Exercicios, new String[]{"Problema", "Nível"});
    }

    public DefaultTableModel obterExerciciosNivel(Nivel nivel) {
        Exercicio[][] Exercicios = null;

        java.util.List<Exercicio> lst = IPVBusinessFacade.obterTodosExerciciosAvulsosPorNivel(nivel);
        Exercicios = new Exercicio[lst.size()][2];

        for (int i = 0; i < Exercicios.length; i++) {
            Exercicios[i] = new Exercicio[]{lst.get(i)};
        }



        return new ExercicioTableModel(Exercicios, new String[]{"Problema", "Nível"});
    }

    public DefaultTableModel obterExerciciosAula() {
        Exercicio[][] Exercicios = null;
        if (SessaoAluno.getInstance().aulaSelecionada != null) {

            java.util.List<Exercicio> lst = IPVBusinessFacade.obterTodosExerciciosPorAula(SessaoAluno.getInstance().aulaSelecionada);
            Exercicios = new Exercicio[lst.size()][2];

            for (int i = 0; i < Exercicios.length; i++) {
                Exercicios[i] = new Exercicio[]{lst.get(i)};
            }

        }

        return new ExercicioTableModel(Exercicios, new String[]{"Problema", "Nível"});
    }

    class ExercicioTableModel extends DefaultTableModel {

        private List<Nivel> nlst;

        public ExercicioTableModel(Object[][] o, Object[] p) {
            super(o, p);
            nlst = IPVBusinessFacade.obterTodosNiveis();
        }

        @Override
        public boolean isCellEditable(int arg0, int arg1) {
            return false;
        }

        @Override
        public Object getValueAt(int row, int col) {
            if (col > 0) {
                Integer id = ((Exercicio) super.getValueAt(row, 0)).getToNivel().getId();
                for (Nivel n : nlst) {
                    if (n.getId().equals(id)) {
                        return n;
                    }
                }

                return "ERRO";
            } else {
                return super.getValueAt(row, col);
            }
        }
    };
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnFiltrar;
    private javax.swing.JButton btnTodos;
    private javax.swing.JComboBox cmbNivel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable tblExerciciosAula;
    private javax.swing.JTable tblExerciciosAvulsos;
    // End of variables declaration//GEN-END:variables
}
